# -*- coding: utf-8 -*-
from xTkinter import * 
import tkFileDialog
import tkMessageBox as msgBox
import sys
from Vue_EditionReglesAffaires import *
from Validation_Contraintes import *
Validateur = ValidationContrainte()
import xmlrpclib

class Edition(PanedWindow):
    def __init__(self,master,parent,**cnf):
        PanedWindow.__init__(self,master,cnf)
        self.configure(orient=VERTICAL,bd=5)
        self.pack(expand='yes', fill=BOTH)
        self.add(Vue_ReglesAffaires(master,parent,self))
        pass
    
class Vue_ReglesAffaires(Frame):
    def __init__(self,*args,**cnf):
        self.ListeContraintes = []
        self.ListLabels = []
        self.ListeBoutons = []
        self.parent = args[1] #parent
        self.master=args[0]#master
        self.panedwindow=args[2]
        Frame.__init__(self,self.master,cnf)             
        self.pack(side=LEFT,anchor=NW)
        self.configure(width=800,height=150)
        self.CBTables=xCombobox(self)
        self.CBTables.setLocation(135,0)
        self.CBTables.setWidth(15)
        self.CBTables.setListWidth(25)
        self.CBTables.setListHeight(5)
        self.obtenirTables() 
        self.CBTables.setValidator(self.obtenirContraintes)
        self.CBTables.setText('<Choisir>')
        
        
    
    def obtenirTables(self):
        column=0 
        Reponse = self.parent.serveur.ObtenirListeTables()
        if(Reponse[0]==1):
            self.ListeChamps = Reponse[1]
            for i in range(len(self.ListeChamps)):
                self.CBTables.addItem(self.ListeChamps[i])
                self.CBTables.setText(self.ListeChamps[i])
        
    def obtenirContraintes(self):
        self.ListeContraintes = []
        Reponse = self.parent.serveur.ObtenirContraintes(self.CBTables.getText())
        print self.CBTables.getText()
        for Label in self.ListLabels:
            Label.destroy()
        for Bouton in self.ListeBoutons:
            Bouton.destroy()
        if(Reponse[0]==1):
            print Reponse
            self.ListeContraintes = Reponse[1]
            Position = 0
            self.ListeBoutons.append(xButton(self,text="Ajouter Contrainte",width=16, command = self.AjouterContrainte ))
            self.ListeBoutons[len(self.ListeBoutons)-1].setLocation(0,(Position+1)*25+5)
            Position = 1
            for Contrainte in self.ListeContraintes:
                pass
                texte = Contrainte['MESSAGE_GUIDE'] 
                texte = texte + '(' + Contrainte['OPERANDE_GAUCHE'] + '.'
                texte = texte + Contrainte['ATTRIBUT_GAUCHE']
                texte = texte + Contrainte['COMPARAISON']
                texte = texte + Contrainte['OPERANDE_DROITE'] + ')'
                self.ListLabels.append(xLabel(self,text=texte, width=len(texte)+2, anchor=SW ))
                self.ListLabels[len(self.ListLabels)-1].setLocation(0,(Position+1)*25+5)
                self.ListeBoutons.append(xButton(self,text="Editer",width=12, command = lambda contrainte=Contrainte : self.OuvrirFenetreEdition(contrainte)))
                self.ListeBoutons[len(self.ListeBoutons)-1].setLocation(500,(Position+1)*25+5)
                self.ListeBoutons.append(xButton(self,text="Supprimer",width=12, command = lambda contrainte=Contrainte : self.SupprimerContrainte(contrainte)))
                self.ListeBoutons[len(self.ListeBoutons)-1].setLocation(600,(Position+1)*25+5)
                Position = Position + 1
                                 
        else:
          msgBox.showerror('PERP 2009 | Édition des tables', 'Une erreur est survenue!\n'+Reponse[1])
        
    def OuvrirFenetreEdition(self, Contrainte):
        self.panedwindow.add(Vue_EditionReglesAffaires(Contrainte, self.master,self))    
    
    
    def MettreAJourContrainte(self, Contrainte):
        if Contrainte['ID_CONTRAINTES'] <> '':
            Contrainte['OPERANDE_GAUCHE'] = self.CBTables.getText()
            print "Mise a jour" ,Contrainte
            where = {'valeur_gauche':'ID_CONTRAINTES', 'valeur_droite':Contrainte['ID_CONTRAINTES']}
            self.parent.serveur.MettreAJour('PERP_CONTRAINTES', Contrainte, [where])
        else:
            Contrainte['OPERANDE_GAUCHE'] = self.CBTables.getText()
            print "ajouter" ,Contrainte
            self.parent.serveur.InsererEnregistrement('PERP_CONTRAINTES', Contrainte)
        self.obtenirContraintes()
    
    def SupprimerContrainte(self, Contrainte):
        print "a effacer " , Contrainte
        where = {'valeur_gauche':'ID_CONTRAINTES', 'valeur_droite':Contrainte['ID_CONTRAINTES']}
        self.parent.serveur.EffacerEnregistrement('PERP_CONTRAINTES', [where])
        self.obtenirContraintes()     
        
    
    def AjouterContrainte(self):
        self.panedwindow.add(Vue_EditionReglesAffaires(None, self.master,self))
        
class Controleur:
    def __init__(self):
        Serveur='http://localhost:8000'
        self.serveur=xmlrpclib.ServerProxy(Serveur)
        root=Tk()
        root.geometry('800x400')
        vi=Edition(root,self)#Vue_ReglesAffaires(root,self)
        root.mainloop()       

Controleur = Controleur()
     
        
    

            



    
    
    
